<?php
/**
 * Smart Library - Smart Framework
 * http://smart-framework.highet.eu
 *
 * @category   SmartL
 * @package    SmartL_Zend
 * @subpackage View
 * @copyright  Copyright (c) 2005-2008 Smart Framework team - Stepan Sindelar, Vojtech Vit
 * @license    http://www.opensource.org/licenses/bsd-license.php	New BSD License
 */

/**
 * @see Zend_View_Helper_Url
 */
require_once 'Zend/View/Helper/Url.php';
/**
 * @see SmartL_Application
 */
require_once 'SmartL/Application.php';

/**
 * Helper for generating urls.
 * This helper adds language to each url, which the original Zend_View_Helper_Url doesn't support.
 *
 * @package SmartL_Zend
 * @subpackage View
 */
class SmartL_Zend_View_Helper_Url extends Zend_View_Helper_Url {
	
	/**
	 * @var SmartL_Application
	 */
	protected $_application = null;
	
	public function __construct($application = null) {
		if ($application !== null)
			$this->_application = $application;
		else
			$this->_application = SmartL_Application::getInstance ();
	}
	
	/**
	 * Generates an url given the name of a route.
	 *
	 * @param  array $urlOptions Options passed to the assemble method of the Route object.
	 * 		Value under index SmartL_Application::URL_LANGUAGE_ID will be used as language. 
	 * 		If it isn't set, language from SmartL_Application will be used.
	 * @param  mixed $name The name of a Route to use. If null it will use the current Route
	 * @param  bool $reset Whether or not to reset the route defaults with those provided
	 * @return string Url for the link href attribute.
	 */
	public function url(array $urlOptions = array(), $name = null, $reset = true, $encode = true) {
		if (! isset ( $urlOptions [SmartL_Application::URL_LANGUAGE_ID] )) {
			$languages = $this->_application->getLanguages ();
			$urlOptions [SmartL_Application::URL_LANGUAGE_ID] = strtolower( $languages [0]->getId () );
		}
		$result = parent::url ( $urlOptions, $name, $reset, $encode );
		if ( $this->_application->getAlwaysLowerUrlFlag() ) {
			$result = strtolower ( $result );
		}
		return $result;
	}
}